/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jtcp;


import com.ysxn.communicate.CommFactory;
import com.ysxn.communicate.socket.CommClientImpl;
import com.ysxn.communicate.socket.SocketClientThread;
import com.ysxn.util.GTools;
import com.ysxn.util.HexTool;
import org.apache.commons.logging.Log;    
import org.apache.commons.logging.LogFactory;  




/**
 *
 * @author stronghill
 */
public class CommFrame extends javax.swing.JInternalFrame {
    private final static Log log = (Log) LogFactory.getLog(CommFrame.class);
    private CommClientImpl mcci = null;

    public int onReceived(byte[] data, int count ){
        String txt = txtRecv.getText();
        txtRecv.setText(txt + HexTool.byteToHex(data, " "));
        return 0;
    }
    /**
     * Creates new form CommFrame
     */
    public CommFrame() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        txtSend = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtRecv = new javax.swing.JTextArea();
        btnConnection = new javax.swing.JToggleButton();
        jLabel1 = new javax.swing.JLabel();
        txtClientIp = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtClientPort = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        cbRecv = new javax.swing.JCheckBox();
        cbSend = new javax.swing.JCheckBox();
        btnSend = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        txtServerIp = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtServerPort = new javax.swing.JTextField();

        setClosable(true);

        txtSend.setColumns(20);
        txtSend.setRows(5);
        jScrollPane1.setViewportView(txtSend);

        txtRecv.setColumns(20);
        txtRecv.setRows(5);
        jScrollPane2.setViewportView(txtRecv);

        btnConnection.setText("连接");
        btnConnection.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConnectionActionPerformed(evt);
            }
        });

        jLabel1.setText("客户端ip");

        jLabel2.setText("客户端port");
        jLabel2.setToolTipText("");

        jLabel3.setText("要发送的内容");

        jLabel4.setText("收到的内容");

        cbRecv.setText("以16进制显示");

        cbSend.setText("以16进制显示");

        btnSend.setText("发送");
        btnSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSendActionPerformed(evt);
            }
        });

        jLabel5.setText("服务端ip");

        jLabel6.setText("服务端port");
        jLabel6.setToolTipText("");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
            .addComponent(jScrollPane2)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cbRecv))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(54, 54, 54)
                                .addComponent(cbSend)
                                .addGap(18, 18, 18)
                                .addComponent(btnSend)))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnConnection)
                        .addGap(43, 43, 43)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtServerIp)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel6))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtClientIp, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel2)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtClientPort, javax.swing.GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE)
                            .addComponent(txtServerPort)))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtClientIp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)
                            .addComponent(txtClientPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(txtServerIp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6)
                            .addComponent(txtServerPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(btnConnection)))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cbSend)
                    .addComponent(btnSend))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(39, 39, 39)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(cbRecv))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(47, 47, 47))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnConnectionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConnectionActionPerformed
        // TODO add your handling code here:
        boolean bmode = btnConnection.getModel().isSelected();
        if(bmode){            
            try {
                title = GTools.getDateTimeStr2(0);
                CommFactory cf = new CommFactory(); 
                setMcci((CommClientImpl)cf.getCommClient());
                getMcci().setCf(this);
                getMcci().connect(txtServerIp.getText(), Integer.valueOf(txtServerPort.getText()));                
                SocketClientThread sct;
                sct = new SocketClientThread(getMcci());
                getMcci().setTpath(title);
                sct.start();
                
                
            } catch (Exception ex) {
                log.error("error ", ex);
            }
            
        }else{
            
        }
    }//GEN-LAST:event_btnConnectionActionPerformed

    private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSendActionPerformed
        // TODO add your handling code here:
        if(getMcci() == null){
            log.info("未连接");
            return;
        }
        txtSend.getText();
        byte[] sd = HexTool.hexStringToBytes(txtSend.getText(),' ');
        getMcci().send(sd, 0, sd.length);
        
    }//GEN-LAST:event_btnSendActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton btnConnection;
    private javax.swing.JButton btnSend;
    private javax.swing.JCheckBox cbRecv;
    private javax.swing.JCheckBox cbSend;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField txtClientIp;
    private javax.swing.JTextField txtClientPort;
    private javax.swing.JTextArea txtRecv;
    private javax.swing.JTextArea txtSend;
    private javax.swing.JTextField txtServerIp;
    private javax.swing.JTextField txtServerPort;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the mcci
     */
    public CommClientImpl getMcci() {
        return mcci;
    }

    /**
     * @param mcci the mcci to set
     */
    public void setMcci(CommClientImpl mcci) {
        this.mcci = mcci;
        //this.setTitle("server " + mcci.getClient().getLocalAddress().getHostAddress());
        //txtServerIp.setText(mcci.getClient().getLocalAddress().getHostAddress());
        //txtServerPort.setText(String.valueOf(mcci.getClient().getPort()));
        
    }
}
